<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
class Orderlist extends Common
{
    
 
    public function index()
    {
       $request = Request::instance();
       $key=$request->only(['key']);
        if($key){
		 $key = $key['key'];
        }else{
		 $key ='';
        }
        $this->assign('testkey',$key);
	    $list = db::view('aricle','title,youhui')
		    ->view('yorder','*','yorder.goodId=aricle.id')
			->where('yorder.ordernum','like',"%".$key."%")
			->order('addtime desc')
			->paginate(10,false,['query' => request()->param()]);
		

        $page = $list->render();


        $this->assign('page', $page);
        $cate = db('ariclecate')->select();
		$this->assign('cate',$cate);
	   
	   
	   
        $this->assign('list',$list);
    	return view();
    }
public function add(){
	  if(request()->isPost()) {
            //构建数组
            $data = input('post.');
	        
			$data['images'] = json_encode($data['images']);
            $data['addtime'] = date("Y-m-d",time());
            db('aricle')->insert($data);
            
		   $this->redirect('index'); 
			 


        }else{
        	$cate = db('ariclecate')->select();
			   $this->assign('cate',$cate);
           $this->assign('info','null');
	return view();
		}
}

public function edit(){
	
	
        if(request()->isPost()) {
            $data = input('post.');
			
		   $aricle = db('aricle')->where('id',$data['id'])->find();
		  
			if($data['img']){
				if($aricle['img']){
				$root=   ROOT_PATH . 'public' . DS .$aricle['img'];
				if(file_exists($root)){

					unlink($root);
				}
				}
			}else{
				
				$data['img'] =  $aricle ['img'];
			}
			
			
			
			$images = json_decode($aricle['images']);
		if(isset($data['images'])){
			for($i=0;$i<count($images);$i++){
				
				array_push($data['images'],$images[$i]);
			}
			$data['images'] = json_encode($data['images']);
			}
			
			
		
	
            db('aricle')->update($data);
           $this->redirect('index'); 
        }else{
            $id=input('id');
			
           $cate = db('ariclecate')->select();
			   $this->assign('cate',$cate);
            $info=db('aricle')->where(array('id'=>$id))->find();
            $this->assign('info',$info);
        
	        $this->assign('images',json_decode($info['images']));   
		
		
            return view();
        }
    
	
}

public function del(){
	$aricle =  db('aricle')->where('id',input('param.id'))->find();
	 db('aricle')->where('id',input('param.id'))->delete();
	 //删除文章。删除图片
	 if($aricle['img']){
	 	$root=   ROOT_PATH . 'public' . DS .$aricle['img'];
		if(file_exists($root)){
			unlink($root);
		}
	 }
	 
	  if($aricle['images']){
	  	foreach(json_decode($aricle['images'])   as $value){
	 	$root=   ROOT_PATH . 'public' . DS .$value;
		if(file_exists($root)){
			unlink($root);
		}
		}
	 }
	 
        $this->redirect('index');
	
}
public function zhiding(){
	
	 $data['id'] = input('param.id');
	 $data['orderischu'] = input('param.value');
	
	 db('yorder')->update($data);
	 $this->redirect('index');
}





public function upload(){

    $files = request()->file();
    foreach($files as $file){
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
              $path =  $info->getFilename();

              return "/uploads/".$path;
         
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }    
    }
 	
	}

	//删除图片
	public function deleteimages(){
		$id = $_POST['id'];
		$name = $_POST['name'];
		
		$user = db('aricle');
		$data = $user->where('id='.$id)->find();
		$images = json_decode($data['images']);
	
		$data2['images'] = array();
		foreach($images as $value){
			
			if($name != $value){
				array_push($data2['images'],$value);
			}
			
		}
			
		
		$data2['id'] = $id;
	$data2['images'] = json_encode($data2['images']);
	 $result =	$user->update($data2);
	 
	 if($result){
	 	$path = ROOT_PATH . 'public' . DS .$name;
	 	 if(file_exists($path)){
			
	 	 	unlink($path);
	 	 }
		//更新成功
		return 1;
	 }else{
	 	
		return 0;
	 }	
		
	}
   
}
